home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 297_01 / pribmpc.c < prev    next >
C/C++ Source or Header  |  1991-12-22  |  7KB  |  293 lines

  1. /* pribmpc.c */
  2. /* machine dependent code for pc and clones */
  3.  
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include "prtypes.h"
  7.  
  8. #define CANTALLOCATE "cant allocate "
  9. #define CRPLEASE "Carriage return please"
  10. #define NOCONFIGFILE "sprolog.inf missing using default configuration"
  11. #define CONFIG_FILE "sprolog.inf"
  12. #define YESUPPER 'Y'
  13. #define YESLOWER 'y'
  14.  
  15. extern FILE *Log_file, *Curr_outfile;
  16. extern void exit_term();
  17.  
  18. /**************************** os_alloc() *********************************/
  19. /* This is the function you use to get memory  from the system.         */
  20. /* Why not use malloc? Because malloc will only allocate up to 64K  on   */
  21. /* some systems when there is another function around for larger      */
  22. /* allocations.                                 */
  23.     
  24. char *os_alloc(how_much)
  25. zone_size_t how_much;
  26. {
  27.     char *ret, *malloc();
  28. #ifdef DEBUG
  29. printf("trying to allocate %u\n",how_much);
  30. #endif
  31.     if((ret = malloc(how_much)) == NULL)
  32.     {
  33.         errmsg(CANTALLOCATE);
  34.         exit_term();
  35.         exit(1);
  36.         return(NULL);/* for stupid finicky compilers and lint */
  37.     }
  38.     else
  39. #ifdef DEBUG 
  40. printf("successful\n");
  41. #endif
  42.         return(ret);
  43. }
  44.  
  45. /******************************************************************************
  46.  Initialise terminal
  47.  This is the function you use to set up your terminal for interaction with
  48.  sprolog. It would change if you wanted to incorporate a line editor for example
  49.  ******************************************************************************/
  50. void ini_term()
  51. {
  52.  
  53. }
  54.  
  55. /******************************************************************************
  56.             function exit_term
  57.  This restores the terminal characteristics.
  58.  ******************************************************************************/
  59. void exit_term()
  60. {
  61.  
  62. }
  63.  
  64. /***************************** errmsg() *********************************
  65.  Output error message.
  66.  On some systems you might want to output a box-surrounded message.
  67.  ******************************************************************************/
  68.  
  69. errmsg(s)
  70. char *s;
  71. {
  72. #if LOGGING_CAPABILITY
  73.     if(Log_file){
  74.       fprintf(Log_file, "%s", s);
  75.         fflush(Log_file);
  76.         }
  77. #endif
  78.     fprintf(stdout, "%s\n", s);
  79. }
  80.  
  81. /************************** tty_getc() *********************************
  82.  Read a char from terminal, no matter what the other flags are.
  83.  ******************************************************************************/
  84.  
  85. int tty_getc()
  86. {
  87. int c;
  88.  
  89. c = getchar();
  90.  
  91. #ifdef LOGGING_CAPABILITY
  92. if(Log_file!=NULL)
  93.   {
  94.   fputc(c,Log_file);
  95.   }
  96. #endif
  97.  
  98. return(c);
  99. }
  100.  
  101. /* raw read one char , with echo */
  102. int tty_getche()
  103. {
  104. #ifdef MARK_WILLIAMS
  105. return getcnb(); /* may not be on your machine */
  106. #else
  107. return getche(); /* may not be on your machine */
  108. #endif
  109. }
  110.  
  111. /************************** tty_pr_string() *********************************
  112.  Output string to the terminal no matter what the other flags are.
  113.  ******************************************************************************/
  114.  
  115. int tty_pr_string(s)
  116. char *s;
  117. {
  118. int len;
  119. #ifdef LOGGING_CAPABILITY
  120. if(Log_file  != NULL)
  121.   {
  122.     fprintf(Log_file,"%s",s);
  123.     fflush(Log_file);
  124.   }
  125. #endif
  126.     len = printf("%s",s);
  127.     fflush(stdout);
  128. return (len);
  129. }
  130.  
  131. /*******************************************************************
  132.             pr_string()
  133.  This will output a string to the current output file.
  134.  It also returns the number of chars output.
  135.  It should be the only way your functions can output a string.
  136.  *******************************************************************/
  137.  
  138. int pr_string(s)
  139. char *s;
  140. {
  141. int len;
  142.  
  143. #if LOGGING_CAPABILITY
  144.     extern FILE *Log_file;
  145.     if (Log_file != NULL)
  146.     {
  147.     fprintf(Log_file, "%s", s);
  148.     fflush(Log_file);
  149.     }
  150. #endif
  151.     len = fprintf(Curr_outfile, "%s", s);
  152.     fflush(stdout);
  153. return(len);
  154. }
  155.  
  156. /**************************** read_config() **********************************
  157.  this reads the file SPROLOG.INF so that the sizes of the various 
  158.   memory zones can be set.
  159.  ************************************************************************/
  160.  
  161. int read_config(hsp, strsp, dsp, tsp, sbsp, tmpsp, rsp, psp)
  162. zone_size_t *hsp, *strsp, *dsp, *tsp, *sbsp, *tmpsp;
  163. int *rsp, *psp;
  164. {
  165. #ifdef MARK_WILLIAMS
  166. long  hs, strs, ds, ts, sbs, tmps;
  167. #endif
  168.     FILE *ifp;
  169.  
  170.     if((ifp = fopen(CONFIG_FILE, "r")) == NULL)
  171.     {
  172.         errmsg(NOCONFIGFILE);
  173.         return(0);
  174.     }
  175. #ifdef MARK_WILLIAMS
  176. fscanf(ifp, "%D %D %D %D %D %D %d %d", &hs, &strs, &ds, &ts, &sbs, &tmps, rsp, psp);
  177.     *hsp = hs;
  178.     *strsp = strs;
  179.     *dsp = ds;
  180.     *tsp = ts;
  181.     *sbsp = sbs;
  182.     *tmpsp = tmps;
  183.  
  184. #else
  185.     fscanf(ifp, "%u %u %u %u %u %u %u %u", hsp, strsp, dsp, tsp, sbsp, tmpsp, rsp, psp);
  186. #endif
  187. #ifdef DEBUG
  188.     printf("%u %u %u %u %u %u %u %u", *hsp, *strsp, *dsp, *tsp, *sbsp, *tmpsp, *rsp, *psp);
  189.     getchar();
  190. #endif
  191.     return(1);
  192. }
  193.  
  194.  
  195. /**************************** more_y_n() **********************************
  196.     Ask for confirmation.
  197.  ************************************************************************/
  198. /* a bit crude ... */
  199. more_y_n()
  200. {
  201.     tty_pr_string("More ?");
  202.     return(read_yes());
  203. }
  204.  
  205. /**************************** read_yes() *********************************
  206.         Return 1 iff user types y
  207. Ignores characters after first non blank.
  208. You could use a dialogue box in a windowing environment.
  209. **************************************************************************/
  210. int read_yes()
  211. {
  212. int c;
  213.  
  214. do
  215.  {
  216.  c = tty_getc();
  217.  }while(isspace(c));
  218.  
  219. while(tty_getc() != '\n');/* read rest of line */
  220.  
  221. if(c == YESLOWER || c == YESUPPER )
  222.   return(1);
  223. else
  224.  return(0);
  225. }
  226.  
  227. #ifdef LINE_EDITOR /* some day ... */
  228. /* this function returns the x and y position of the cursor.
  229.    It's not used but you might want to make use of it to extend
  230.    the input.
  231.  */
  232.  
  233. #ifdef __TURBOC__
  234. getxy(px, py)
  235. int *px, *py;
  236. {
  237. *px = wherex();
  238. *py = wherey();
  239. }
  240. #else
  241.  
  242. #include <dos.h>
  243. void gotoxy(X, Y)
  244. short X, Y;
  245. {
  246. union REGS inregs;
  247. union REGS outregs;
  248.            /*
  249. inregs.h.ah = 0x0F;
  250. int86(16, &inregs, &outregs);
  251.              */
  252. inregs.h.ah = 0x02;
  253. inregs.h.bh = 0 ;/* outregs.h.bh; the active page  */
  254. inregs.h.dh = Y;
  255. inregs.h.dl = X;
  256. int86(16/* video */, &inregs, &outregs);
  257. }
  258.  
  259. void getxy(px, py)/* get cursor position */
  260. short *px, *py;
  261. {
  262. union REGS inregs, outregs;
  263.  
  264. inregs.h.ah = 0x0F;
  265. int86(16, &inregs, &outregs);
  266.  
  267. inregs.h.bh = outregs.h.bh; 
  268. inregs.h.ah = 0x03;
  269.  
  270. int86(16, &inregs, &outregs);
  271. *py = outregs.h.dh;
  272. *px = outregs.h.dl;
  273. }
  274. #endif
  275. #endif
  276.  
  277. #ifdef __TURBOC__
  278. #include <time.h>
  279. /* in hundreths of a second */
  280. long clock()
  281. {
  282. return (100* clock()) /CLK_TCK;
  283. }
  284. #else
  285. long  clock()
  286. {
  287. errmsg(" Clock not implemented  on  PC");
  288. /* some recent C User Journal article has a version, I think. */
  289. return(0L);
  290. }
  291. #endif
  292. /* end of file */
  293.